/*
 * @Author: Jiraiya
 * @Date: 2020-04-07 18:54:23
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-04-08 11:21:55
 * @Description: 
 */
import Base from './base'
const his = window.history;
class History{
    constructor() {
        this.routes = {};
        this._bindPopState();
    }

    init(path){
        history.replaceState({path:path}, null, path);
        this.routes[path] && this.routes[path]();
    }

    route(path, callback){
        this.routes[path] = callback || function(){};
    }

    go(path){
        history.replaceState({path}, null, path);
        this.routes[path] && this.routes[path]();
    }

    backOff(){
        history.back();
    }

   _bindPopState(){
       // 监听前进后退
       window.addEventListener('popstate', e => {
           console.log(e)
           const path = e.state && e.state.path;
           this.routes[path] && this.routes[path]();
       })
   }
}

export default History;